package LogicaPresentacion;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Observable;
import java.util.Observer;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JInternalFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.AbstractTableModel;
import LogicaNegocio.Aula;
import LogicaNegocio.GestionAdministrador;

public class verAulas extends JInternalFrame implements ActionListener, Observer
{
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private GestionAdministrador GAd;
	private JTable tbAulas;
	private JScrollPane scrollPane; 
    private JPanel jpBotones;		
	private JButton Aceptar;
	private String Eleccion;
	private TablaAulas tabla = null;       
		
	public verAulas(GestionAdministrador GAd,String Eleccion, ClaseAObservar observable)
	{
		
		super("Aulas", 
	              true, 
	              false, 
	              true, 
	              true);
		setVisible(true);
		setSize(800,300);
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.inicializarSubComponentes();
		this.Eleccion = Eleccion;
		this.GAd = GAd;
		blnCargarFormulario();
	}
	
	public void blnCargarFormulario()
	{
		crearTablaAulas();
		this.setLocation(200,200);
		this.setVisible(true);
		inicializarSubComponentes();
	}
	
	public void crearTablaAulas()
	{
		
		tbAulas=null;
		tabla=new TablaAulas();
		tabla.CrearTabla(GAd.GetAulas());
		tbAulas = new JTable(tabla);
		tbAulas.setPreferredScrollableViewportSize(new Dimension(500, 70));
		tbAulas.setFillsViewportHeight(true);
		tbAulas.setEnabled(true);
		tbAulas.setRowSelectionAllowed(true);
				
	}
	public void inicializarSubComponentes()
	{			
			
		scrollPane= new JScrollPane(tbAulas);

		this.getContentPane().setLayout(new BorderLayout());
        this.getContentPane().add(scrollPane,"Center");
        
        Aceptar=new JButton("Aceptar");
		Aceptar.addActionListener(this);
      
        
		jpBotones=new JPanel();
		jpBotones.setLayout(new FlowLayout());
		jpBotones.add(Aceptar);
		
		
		this.getContentPane().add(jpBotones,"South");
		
	}
		
	public void actionPerformed(ActionEvent e)
	{	
		if (e.getSource() instanceof JButton) 
		{
			JButton btnPulsado=(JButton)e.getSource();

			if (btnPulsado==Aceptar) 
			{
				dispose();
			}					
		}
	}

	public void update(Observable o, Object arg) 
	{
		tbAulas=null;
		tabla.CrearTabla(GAd.GetAulas());
		tbAulas = new JTable(tabla);
		tbAulas.setPreferredScrollableViewportSize(new Dimension(500, 70));
		tbAulas.setFillsViewportHeight(true);
		tbAulas.setEnabled(true);
		tbAulas.setRowSelectionAllowed(true);
		
	}
	
 class TablaAulas extends AbstractTableModel
	 {
		 /**
		 * 
		 */
		private static final long serialVersionUID = 6056384139002577999L;
		private String[] columnNames = {"ID_Aula", "Numero de Aula","Aforo", "Campus"};
		private Object[][] data;
	        
        public void CrearTabla(ArrayList<Aula> Aulas)
        {
        	
    		int filas = Aulas.size();
    		data=new Object[filas][];

			if (Eleccion == "Aforo")
    		{
    			Aulas = GAd.OrdenarAulas(Aulas);
    		}
			
    		for (int cont = 0;  cont<Aulas.size();cont++)
    		{
    			Aula A = (Aula) Aulas.get(cont);
    			Object[]a={
    					   new String(Integer.toString(A.getID())),
    					   new String (Integer.toString(A.getNumeroAula())),
    					   new String(A.getAforoAula()),
    					   new String(A.getNombreCampus()),
    			};
    			data[cont]=a;
    		}	    			        	
        }
        
       public int getColumnCount() 
        {
            return columnNames.length;
        }

        public int getRowCount() {
            return data.length;
        }

        public String getColumnName(int col) 
        {
            return columnNames[col];
        }

        public Object getValueAt(int row, int col) 
        {
            return data[row][col];
        } 
    }
}
